package user;

import book.Book;
import book.Library;
import utils.ScannerSingleton;

import java.time.LocalDate;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 九转苍翎
 * Date: 2025/7/18
 * Time: 02:12
 */
public class Admin extends User {
    public Scanner scanner;
    public Library library = Library.getLibrary();

    public Admin(String name, int ID) {
        super(name, ID, "管理员");
        scanner = ScannerSingleton.getScanner();
    }

    @Override
    public int menu() {
        System.out.println("管理员 " + this.getName() + " 的操作菜单");
        System.out.println("1.查找图书");
        System.out.println("2.打印所有图书");
        System.out.println("3.退出系统");
        System.out.println("4.上架图书");
        System.out.println("5.修改图书");
        System.out.println("6.下架图书");
        System.out.println("7.统计借阅次数");
        System.out.println("8.查看最受欢迎的前K本书");
        System.out.println("9.查看库存状态");
        System.out.println("10.检查超过一年未下架的图书");
        System.out.println("请选择你的操作：");
        return scanner.nextInt();
    }
    //上架图书
    public void addBook() {
        scanner.nextLine();//处理回车
        System.out.println("请输⼊书名：");
        String title = scanner.nextLine();//输⼊书名
        System.out.println("请输⼊作者：");
        String author = scanner.nextLine();//输⼊作者
        System.out.println("请输⼊类别：");
        String category = scanner.nextLine();//输⼊图书类别
        System.out.println("请输⼊出版年份：");
        int year = scanner.nextInt(); //输⼊出版年份
        scanner.nextLine();//处理回车
        LocalDate shelfDate = LocalDate.now();//当前时间作为上架时间
        Book newBook = new Book(title, author, category, year, shelfDate);//创建新书对象
        //调用图书类添加图书
        library.addBook(newBook);
    }
    //图书修改支持修改书名作者类别
    public void updateBook() {
        library.displayBook();
        System.out.println("请输入要修改图书的ID");
        int bookID = scanner.nextInt();
        scanner.nextLine();
        Book book = library.searchById(bookID);
        if (book == null) {
            return;
        }
        System.out.println("当前书名：" + book.getTitle());
        System.out.println("请输⼊新的书名：");
        String newTitle = scanner.nextLine();//输⼊新的书名
        System.out.println("当前作者：" + book.getAuthor());
        System.out.println("请输⼊新的作者：");
        String newAuthor = scanner.nextLine();//输⼊新的作者
        System.out.println("当前类别：" + book.getCategory());
        System.out.println("请输⼊新的类别：");
        String newCategory = scanner.nextLine();//输⼊新的类别
        //更新对应书籍的信息
        book.setTitle(newTitle);
        book.setAuthor(newAuthor);
        book.setCategory(newCategory);
        library.updateBook(book);
    }
    //删除书籍
    public void removeBook() {
        library.displayBook();
        System.out.println("请输入要删除的图书的ID");
        int bookID = scanner.nextInt();
        scanner.nextLine();
        Book book = library.searchById(bookID);
        if (book == null) {
            return;
        }
        library.removeBook(bookID);
        System.out.println("图书：" +  book.getTitle() + " 已被删除");
    }
    //统计每本书的借阅次数
    public void borrowCount() {
        library.borrowCount();
    }
    //查询最受欢迎的前K本书
    public void generateBook() {
        System.out.println("请输⼊你要查看的最受欢迎的前K本书,注意k值不能超 过："+library.bookCount);
        int k = scanner.nextInt();
        if (k <= 0 || k > library.bookCount) {
            System.out.println("没有最受欢迎的前" + k + "本书");
        }
        library.generateBook(k);
    }
    //查看库存状态
    public void checkInventoryStatus() {
        library.checkInventoryStatus();
    }
    //移除上架超过1年的书籍
    public void checkAndRemoveOldBooks() {
        library.checkAndRemoveOldBooks();
    }
}
